package com.everflourish.yeah100.utils.collections;

import com.everflourish.yeah100.entity.statistics.StudentScoreInfo;
import com.everflourish.yeah100.utils.StringUtil;

import java.text.Collator;
import java.util.Comparator;
import java.util.Locale;

/**
 * 统计分析-成绩排序
 *
 * @author pf
 * @date 2015-3-16
 */
public class GradeCollections implements Comparator<StudentScoreInfo> {

    /**
     * 比较学生学号的标记
     */
    public static final int COMPARE_STUDENT_NO = 1;
    /**
     * 比较学生姓名的标记
     */
    public static final int COMPARE_STUDENT_NAME = 2;
    /**
     * 比较学生成绩的标记
     */
    public static final int COMPARE_STUDENT_SCORE = 3;
    /**
     * 比较学生排名的标记
     */
    public static final int COMPARE_STUDENT_RANKING = 4;

    private int mFlag = COMPARE_STUDENT_NO;// 标记，默认为学号
    // 是否是降序排序
    private boolean isDesc = false;

    public GradeCollections(boolean isDesc, int flag) {
        this.isDesc = isDesc;
        this.mFlag = flag;
    }

    @Override
    public int compare(StudentScoreInfo lhsStudentScoreInfo,
                       StudentScoreInfo rhsStudentScoreInfo) {
        int compareFlag = 0;
        switch (mFlag) {
            case COMPARE_STUDENT_NO:
                if (isDesc) {
                    compareFlag = -lhsStudentScoreInfo
                            .getStudentNo()
                            .compareToIgnoreCase(rhsStudentScoreInfo.getStudentNo());
                } else {
                    compareFlag = lhsStudentScoreInfo
                            .getStudentNo()
                            .compareToIgnoreCase(rhsStudentScoreInfo.getStudentNo());
                }
                break;
            case COMPARE_STUDENT_NAME:
                String lhsName = lhsStudentScoreInfo.getStudentName();
                String rhsName = rhsStudentScoreInfo.getStudentName();
                if (isDesc) {
                    compareFlag = -Collator.getInstance(Locale.CHINESE).compare(
                            lhsName, rhsName);

                } else {
                    compareFlag = Collator.getInstance(Locale.CHINESE).compare(
                            lhsName, rhsName);
                }
                break;
            case COMPARE_STUDENT_SCORE:
                String lhsScoreStr = lhsStudentScoreInfo.getScore();
                String rhsScoreStr = rhsStudentScoreInfo.getScore();
                double lhsScore = StringUtil.parseDouble(lhsScoreStr, -1);
                double rhsScore = StringUtil.parseDouble(rhsScoreStr, -1);
                if (isDesc) {
                    if (lhsScore > rhsScore) {
                        compareFlag = -1;
                    } else if (lhsScore < rhsScore) {
                        compareFlag = 1;
                    } else {
                        compareFlag = lhsStudentScoreInfo.getStudentNo()
                                .compareToIgnoreCase(
                                        rhsStudentScoreInfo.getStudentNo());
                    }
                } else {
                    if (lhsScore > rhsScore) {
                        compareFlag = 1;
                    } else if (lhsScore < rhsScore) {
                        compareFlag = -1;
                    } else {
                        compareFlag = lhsStudentScoreInfo.getStudentNo()
                                .compareToIgnoreCase(
                                        rhsStudentScoreInfo.getStudentNo());
                    }
                }
                break;
            case COMPARE_STUDENT_RANKING:
                String lhsGradeRanking = lhsStudentScoreInfo.getGradeRanking();
                String rhsGradeRanking = rhsStudentScoreInfo.getGradeRanking();
                if (StringUtil.parseInt(lhsGradeRanking, -1) == -1) {
                    lhsGradeRanking = null;
                }
                if (StringUtil.parseInt(rhsGradeRanking, -1) == -1) {
                    rhsGradeRanking = null;
                }
                if (isDesc) {
                    if (lhsGradeRanking == null && rhsGradeRanking != null) {
                        compareFlag = 1;
                    } else if (lhsGradeRanking != null && rhsGradeRanking == null) {
                        compareFlag = -1;
                    } else if (lhsGradeRanking != null && rhsGradeRanking != null) {
                        int lhsRanking = Integer.parseInt(lhsGradeRanking);
                        int rhsRanking = Integer.parseInt(rhsGradeRanking);
                        if (lhsRanking > rhsRanking) {
                            compareFlag = -1;
                        } else if (lhsRanking < rhsRanking) {
                            compareFlag = 1;
                        } else {
                            compareFlag = lhsStudentScoreInfo.getStudentNo()
                                    .compareToIgnoreCase(
                                            rhsStudentScoreInfo.getStudentNo());
                        }
                    } else {
                        compareFlag = -lhsStudentScoreInfo.getStudentNo()
                                .compareToIgnoreCase(
                                        rhsStudentScoreInfo.getStudentNo());
                    }
                } else {
                    if (lhsGradeRanking == null && rhsGradeRanking != null) {
                        compareFlag = 1;
                    } else if (lhsGradeRanking != null && rhsGradeRanking == null) {
                        compareFlag = -1;
                    } else if (lhsGradeRanking != null && rhsGradeRanking != null) {
                        int lhsRanking = Integer.parseInt(lhsGradeRanking);
                        int rhsRanking = Integer.parseInt(rhsGradeRanking);
                        if (lhsRanking > rhsRanking) {
                            compareFlag = 1;
                        } else if (lhsRanking < rhsRanking) {
                            compareFlag = -1;
                        } else {
                            compareFlag = lhsStudentScoreInfo.getStudentNo()
                                    .compareToIgnoreCase(
                                            rhsStudentScoreInfo.getStudentNo());
                        }
                    } else {
                        compareFlag = lhsStudentScoreInfo.getStudentNo()
                                .compareToIgnoreCase(
                                        rhsStudentScoreInfo.getStudentNo());
                    }
                }
                break;
        }
        return compareFlag;
    }
}
